using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;

public class IntersectionObjManager
{
    public static IntersectionObjManager Instance = new IntersectionObjManager();

    bool m_IsInited = false;

    //List<QuiQuiObj> m_QuiQuis = new List<QuiQuiObj>();
    //float m_GenerateSize = 45;

    Dictionary<IntersectionObjType, string> m_IntersectionObjPrefabMap = new Dictionary<IntersectionObjType, string>();

    public enum IntersectionObjType
    {
        None,
        SmallFood,
        BigFood,
        TecChat,
        Cropland,
        Shit
    }
    IntersectionObjManager()
    {
        m_IntersectionObjPrefabMap.Add(IntersectionObjType.SmallFood, "Prefabs/IntersectionObj/SmallFood");
        m_IntersectionObjPrefabMap.Add(IntersectionObjType.BigFood, "Prefabs/IntersectionObj/BigFood");
        m_IntersectionObjPrefabMap.Add(IntersectionObjType.TecChat, "Prefabs/IntersectionObj/TecChat");
        m_IntersectionObjPrefabMap.Add(IntersectionObjType.Cropland, "Prefabs/IntersectionObj/Cropland");
        m_IntersectionObjPrefabMap.Add(IntersectionObjType.Shit, "Prefabs/IntersectionObj/Shit");
    }
    public void Init()
    {
        if (m_IsInited)
            return;
        m_IsInited = true;
        m_IntersectionObjects.Add(IntersectionObjType.SmallFood, new List<IntersectionObject>());
    }
    bool m_IsTimeCount = false;
    int m_ScatterFoodNumber = 20;
    Dictionary<IntersectionObjType, List<IntersectionObject>> m_IntersectionObjects = new Dictionary<IntersectionObjType, List<IntersectionObject>>();
    public void Update()
    {
        if (!m_IsTimeCount)
        {
            m_IsTimeCount = true;
            DOVirtual.DelayedCall(1f, () =>
            {
                ScatterFood();
                m_IsTimeCount = false;
            });
        }

        var smallFoodList = m_IntersectionObjects[IntersectionObjType.SmallFood];
        for (int i = (smallFoodList.Count - 1); i >= 0; --i)
        {
            if (smallFoodList[i].gameObject.activeSelf == false)
            {
                smallFoodList.RemoveAt(i);
            }
        }
    }

    public List<IntersectionObject> GetIntersectionObjects(IntersectionObjType type)
    {
        return m_IntersectionObjects[type];
    }
    public void ScatterFood()
    {
        var size = WorldManager.Instance.worldSize;
        for (int i = 0; i < m_ScatterFoodNumber; ++i)
        {
            var newObj = CreateObj(IntersectionObjType.SmallFood);
            newObj.transform.position = new Vector3(Random.Range(-size / 2f, size / 2f), Random.Range(-size / 2f, size / 2f), 0);
            m_IntersectionObjects[IntersectionObjType.SmallFood].Add(newObj.GetComponent<FoodObj>());
        }
    }
    public GameObject CreateObj(IntersectionObjType type)
    {
        GameObject retObj = null;
        if (m_IntersectionObjPrefabMap.ContainsKey(type))
        {
            retObj = Pool.GetInstance().GetObj(m_IntersectionObjPrefabMap[type]);
            retObj.GetComponent<IntersectionObject>().OnCreate();
        }
        else
        {
            Debug.LogError("not found prefab from " + type.ToString() + "in IntersectionObjManager:CreateObj");
        }

        return retObj;
    }
    public void CycleObj(GameObject obj)
    {
        Pool.GetInstance().DestroyObj(obj);
    }
}
